package com.four.peoplemanage.overtime.dao;

import com.four.peoplemanage.overtime.entity.Overtime;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface OverDao {
    //申请加班
    @Insert("insert into overtime (information_id,name,overtime_type,overbegintime,overendtime,content,status)"+
    "values (#{information_id},#{name},#{overtime_type},#{overbegintime},#{overendtime},#{content},'待审核')")
    public int addOvertime(Overtime overtime);

    //分页查询
    @Select("<script>" +
            "SELECT * FROM overtime" +
            "  <where>" +
            "    <if test=\" name != null\">" +
            "       name = #{name} " +
            "    </if>" +
            "    <if test=\" overtime_type != null\">" +
            "       and overtime_type = #{overtime_type} " +
            "    </if>" +
            "    <if test=\" status != null\">" +
            "       and status = #{status} " +
            "    </if>" +
            "       and information_id = #{information_id}" +
            "  </where>" +
            "  ORDER BY CASE status WHEN '待审核' THEN 1 ELSE 2 END, overbegintime ASC " +
            "limit #{start}, #{size}" +
            "</script>")
    public List<Overtime> getOvertime(int start, int size, String name, String overtime_type,int information_id,
                                      String status);
    //分页查询
    @Select("<script>" +
            "SELECT * FROM overtime" +
            "  <where>" +
            "    <if test=\" name != null\">" +
            "       name = #{name} " +
            "    </if>" +
            "    <if test=\" overtime_type != null\">" +
            "       and overtime_type = #{overtime_type} " +
            "    </if>" +
            "    <if test=\" status != null\">" +
            "       and status = #{status} " +
            "    </if>" +
            "  </where>" +
            "  ORDER BY CASE status WHEN '待审核' THEN 1 ELSE 2 END, overbegintime ASC " +
            "limit #{start}, #{size}" +
            "</script>")
    public List<Overtime> getAllOvertime(int start, int size, String name, String overtime_type,
                                      String status);

    @Select("<script>" +
            "select count(*) from overtime" +
            "  <where>" +
            "    <if test=\" name != null\">" +
            "       name = #{name} " +
            "    </if>" +
            "    <if test=\" overtime_type != null\">" +
            "       and overtime_type = #{overtime_type} " +
            "    </if>" +
            "    <if test=\" status != null\">" +
            "       and status = #{status} " +
            "    </if>" +
            "</where>"+
            "</script>")
    public  int countOvertime(String name, String overtime_type,
                              String status);

    //更新
    @Update("<script>" +
            "update  overtime " +
            "        <set>" +
            "            <if test=\"null!= status\">" +
            "                status =#{status}," +
            "            </if>" +
            "            <if test=\"null!= overtime_type\">" +
            "                overtime_type =#{overtime_type}," +
            "            </if>" +
            "            <if test=\"null!= content\">" +
            "                content =#{content}," +
            "            </if>" +
            "        </set>" +
            "        where id =#{id}" +
            "</script>")
    public int updateOvertimeById(Overtime overtime);

    @Update("<script>" +
            "update  overtime " +
            "        <set>" +
            "            <if test=\"null!= status\">" +
            "                status =#{status}," +
            "            </if>" +
            "        </set>" +
            "        where id =#{id}" +
            "</script>")
    public int updateOvertime(Overtime overtime);

}
